package com.frejoys.app.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.app.AddUserCashDto;
import com.frejoys.common.dto.app.UserWalletLogDto;
import com.frejoys.common.dto.app.UserWalletLogTransferDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.MoneyDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.enums.EUserRole;
import com.frejoys.common.enums.EUserWalletLogType;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.CommonUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.common.vo.app.*;
import com.frejoys.common.vo.common.UserCashInfoVo;
import com.frejoys.service.CommonService;
import com.frejoys.service.UserCashService;
import com.frejoys.service.UserWalletLogService;
import com.frejoys.service.UserWalletService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "用户钱包", description = "用户钱包")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixApp)
@Authorization(Authority.Private)
public class UserWalletController {

    private final UserWalletService userWalletService;

    private final UserWalletLogService userWalletLogService;

    private final CommonService commonService;

    private final UserCashService userCashService;

    private final RedissonClient redissonClient;

//    @PostMapping("/userWallet/userWalletInfo")
//    @Operation(summary = "用户钱包信息")
//    public Result<UserWalletVo> userWalletInfo(){
//        return Result.success(userWalletService.userWalletInfo(RequestUtil.userId()));
//    }

//    @PostMapping("/userWallet/userPromotionWalletInfo")
//    @Operation(summary = "达人钱包信息")
//    public Result<UserPromotionWalletVo> userPromotionWalletInfo(){
//        return Result.success(userWalletService.userWalletInfo(RequestUtil.userId()));
//    }


    //todo 接口不用 暂时放开避免前端报错
    @PostMapping("/userWalletLog/userWalletListInfo")
    @Operation(summary = "收益记录")
    public Result<List<UserWalletLogListVo>> userWalletListInfo(@RequestBody @Validated PageDto dto){
        return Result.success();
    }

//    @PostMapping("/userWalletLog/userConsumptionWalletListInfo")
//    @Operation(summary = "消费记录")
//    public Result<List<UserWalletLogListVo>> userConsumptionWalletListInfo(@RequestBody @Validated PageDto dto){
//        return Result.success(userWalletLogService.userConsumptionWalletListInfo(RequestUtil.userId(),dto));
//    }

    @PostMapping("/userWalletLog/cash")
    @Operation(summary = "用户申请提现")
    public Result userWalletCash(@RequestBody @Validated MoneyDto dto){
        commonService.limitationMoney(dto.getMoney());
        // 保留2位小数
        dto.setMoney(CommonUtil.bigDecimalRoundDown(dto.getMoney()));
        userWalletLogService.walletCash(RequestUtil.userId(),dto);
        return Result.success();
    }


    @PostMapping("/userWalletLog/addUserCash")
    @Operation(summary = "添加提现账号")
    public Result addUserCash(@RequestBody @Validated AddUserCashDto dto){

        RLock lock = redissonClient.getLock(CacheKeyUtil.userCashLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                userCashService.addUserCash(dto,RequestUtil.userId());
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }


    @PostMapping("/userWalletLog/userCashInfo")
    @Operation(summary = "查询提现账号信息")
    public Result<UserCashInfoVo> userCashInfo(){
        return Result.success(userCashService.userCashInfo(RequestUtil.userId()));
    }




    @PostMapping("/userWalletLog/userWalletRemainingSum")
    @Operation(summary = "余额记录列表")
    public Result<List<UserWalletLogListVo>> userWalletRemainingSum(@RequestBody @Validated UserWalletLogDto dto){
        return Result.success(userWalletLogService.userWalletRemainingSum(RequestUtil.userId(),dto));
    }

    @PostMapping("/userWalletLog/freeOrderList")
    @Operation(summary = "排队免单记录列表")
    public Result<List<UserWalletLogListVo>> freeOrderList(@RequestBody @Validated UserWalletLogDto dto){
        return Result.success(userWalletLogService.userWalletLogListInfo(RequestUtil.userId(),dto, EUserWalletLogType.orderRebate.getValue(),EUserWalletLogType.outRebate.getValue()));
    }

    @PostMapping("/userWalletLog/promotionBrokerage")
    @Operation(summary = "达人推广佣金列表")
    public Result<List<UserWalletLogListVo>> promotionBrokerage(@RequestBody @Validated UserWalletLogDto dto){
        return Result.success(userWalletLogService.userWalletLogListInfo(RequestUtil.userId(),dto,EUserWalletLogType.promotionRebate.getValue(),EUserWalletLogType.outPromotion.getValue()));
    }


    @PostMapping("/userWalletLog/agentBrokerage")
    @Operation(summary = "代理推广佣金列表")
    public Result<List<UserWalletLogListVo>> agentBrokerage(@RequestBody @Validated UserWalletLogDto dto){
        return Result.success(userWalletLogService.userWalletLogListInfo(RequestUtil.userId(),dto,EUserWalletLogType.agentRebate.getValue(),EUserWalletLogType.groupRebate.getValue()));
    }

    @PostMapping("/userWalletLog/consumptionRecord")
    @Operation(summary = "消费记录")
    public Result<List<UserWalletLogListVo>> consumptionRecord(@RequestBody @Validated UserWalletLogDto dto){
        return Result.success(userWalletLogService.userWalletLogListInfo(RequestUtil.userId(),dto,EUserWalletLogType.order.getValue(),EUserWalletLogType.convertScore.getValue()));
    }

    @PostMapping("/userWalletLog/userWalletMoney")
    @Operation(summary = "余额")
    public Result<UserWalletMoneyVo> userWalletMoney(){
        return Result.success(userWalletService.userWalletMoney(RequestUtil.userId()));
    }


    @PostMapping("/userWalletLog/freeOrderDetails")
    @Operation(summary = "排队免单")
    public Result<FreeOrderDetailsVo> freeOrderDetails(){
        return Result.success(userWalletService.freeOrderDetails(RequestUtil.userId()));
    }


    @PostMapping("/userWalletLog/promotionBrokerageDetails")
    @Operation(summary = "达人推广佣金")
    public Result<PromotionBrokerageDetailsVo> promotionBrokerageDetails(){
        return Result.success(userWalletService.promotionBrokerageDetails(RequestUtil.userId()));
    }

    @PostMapping("/userWalletLog/agentBrokerageDetails")
    @Operation(summary = "代理推广佣金")
    public Result<AgentBrokerageDetailsVo> agentBrokerageDetails(){
        return Result.success(userWalletService.agentBrokerageDetails(RequestUtil.userId()));
    }



    @PostMapping("/userWalletLog/userWalletDetails")
    @Operation(summary = "用户钱包")
    public Result<UserWalletInfoVo> userWalletDetails(){
        return Result.success(userWalletService.userWalletDetails(RequestUtil.userId()));
    }

    @PostMapping("/userWalletLog/userWalletLogDetails")
    @Operation(summary = "消费记录详情")
    public Result<UserWalletLogDetailsVo> userWalletLogDetails(@RequestBody @Validated IdDto dto){
        return Result.success(userWalletLogService.userWalletLogDetails(dto));
    }

    @PostMapping("/userWalletLog/transferWallet")
    @Operation(summary = "返利、代理返利，达人返利钱包转入余额")
    public Result transferWallet(@RequestBody @Validated UserWalletLogTransferDto dto){
        userWalletLogService.transferWallet(RequestUtil.userId(), dto);
        return Result.success();
    }


}
